<template>
  <div class="detail-page">
    <van-nav-bar left-text="返回" @click-left="$router.back()" fixed title="面经详情" />
    <header class="header">
      <h1>{{ detail.stem || '大标题' }}</h1>

      <p>
        {{ detail.createdAt }} | {{ detail.views }} 浏览量 | {{ detail.likeCount }} 点赞数
      </p>

      <p>
        <img :src="detail.avatar" alt="作者头像" />
        <span>{{ detail.creator }}</span>

      </p>

    </header>

    <main class="body" v-html="detail.content">

    </main>

    <div class="opt">
      <van-icon :class="detail.likeFlag?'active':''"  name="like-o" @click="opt(1)"/>
      <van-icon :class="detail.collectFlag?'active':''"  name="star-o" @click="opt(2)"/>
    </div>

  </div>

</template>

<script>
import { getDetailAPI, optAPI } from '@/api/article'
import { Toast } from 'vant'

export default {
  name: 'DetailPage',
  data () {
    return {
      detail: {}
    }
  },
  created () {
    this.getDetail()
  },
  methods: {
    async getDetail () {
      const res = await getDetailAPI(this.$route.params.id)
      console.log('面经详情', res)
      this.detail = res.data
    },
    async opt (type) {
      const res = await optAPI({
        id: this.detail.id,
        optType: type
      })
      console.log('点赞/收藏', res)
      if (type === 1) {
        // 点赞
        if (this.detail.likeFlag) {
          this.detail.likeFlag = 0
          this.detail.likeCount--
        } else {
          this.detail.likeFlag = 1
          this.detail.likeCount++
        }
      } else {
        // 收藏
        if (this.detail.collectFlag) {
          this.detail.collectFlag = 0
          Toast('取消收藏成功')
        } else {
          this.detail.collectFlag = 1
          Toast('收藏成功')
        }
      }
      this.getDetail()
    }
  }
}
</script>

<style lang="less" scoped>
.detail-page {
  margin-top: 44px;
  overflow: hidden;
  padding: 0 15px;

  .header {
    h1 {
      font-size: 24px;
    }

    p {
      color: #999;
      font-size: 12px;
      display: flex;
      align-items: center;
    }

    img {
      width: 40px;
      height: 40px;
      border-radius: 50%;
      overflow: hidden;
    }
  }

  .opt {
    position: fixed;
    bottom: 100px;
    right: 0;

    >.van-icon {
      margin-right: 20px;
      background: #fff;
      width: 40px;
      height: 40px;
      line-height: 40px;
      text-align: center;
      border-radius: 50%;
      box-shadow: 2px 2px 10px #ccc;
      font-size: 18px;

      &.active {
        background: #FEC635;
        color: #fff;
      }
    }
  }
}
</style>
